package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.Resource;
import com.biovr.backend.repository.ResourceRepository;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by monodev on 2017/6/4.
 */
@Api(value = "/resources",description = "资源API", consumes="application/json")
@RestController
@RequestMapping("/resources")
public class ResourceRestController {
    @Autowired
    private ResourceRepository resourceRepository;

    /**
     * 获得某个表记录的所有资源
     * @param tableName
     * @param tableId
     * @return
     */
    @ApiOperation(value="获得某个表记录的所有资源",response = RestResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名称，必须是expirement、apparatus、reagent、custom之一", required = true, dataType = "string",paramType="path"),
            @ApiImplicitParam(name = "tableId", value = "表Id", required = true, dataType = "string",paramType="path")
    })
    @GetMapping("/{tableName}/{tableId}")
    public RestResponse<List<Resource>> getResourcesOfTable(
            @PathVariable String tableName, @PathVariable String tableId){
        List<Resource> res =resourceRepository.findByTableNameAndTableId(tableName,tableId);
        return  ResponseFactory.createRestResponse(true,"",res);
    }

    /**
     * 添加/更新资源
     * @param resource
     * @return
     */
    @ApiOperation(value="添加/更新资源",response = RestResponse.class)
    @ApiImplicitParam(name = "resource", value = "资源对象", required = true, dataType = "Resource")
    @PostMapping
    public  RestResponse<Resource> saveOne(@RequestBody Resource resource) throws Exception{
        Resource res =  resourceRepository.save(resource);
        return  ResponseFactory.createSuccessResult(res);
    }

    /**
     * 将某个表某个类别的资源设为主文档
     * @param id
     * @return
     */
    @ApiOperation(value="将某个表某个类别的资源设为主文档",response = RestResponse.class)
    @ApiImplicitParam(name = "id", value = "资源id", required = true, dataType = "String",paramType = "path")
    @PostMapping("/main/{id}")
    public RestResponse<Boolean> setMainReource(@PathVariable String id) throws Exception{
        Resource res = resourceRepository.findOne(id);
        if(null!=res){
            List<Resource> allOfThisType=resourceRepository.findByTableNameAndTableIdAndType(
                    res.getTableName(),res.getTableId(),res.getType());
            if(null!=allOfThisType){
                for(Resource r: allOfThisType){
                    r.setMainFile(false);
                    resourceRepository.save(r);
                }
            }
            res.setMainFile(true);
            resourceRepository.save(res);
        }
        return ResponseFactory.createSuccessResult();
    }

    /**
     * 批量添加/更新
     * @param resources
     * @return
     */
    @ApiOperation(value="批量添加/更新",response = RestResponse.class)
    @ApiImplicitParam(name = "resources", value = "资源列表", required = true, dataType = " List<Resource>", paramType = "query")
    @PostMapping("/batch")
    public  RestResponse<?> saveBatch(@RequestBody List<Resource> resources) throws Exception{
        for (Resource r:resources){
            Resource res =  resourceRepository.save(r);
        }
        return  ResponseFactory.createSuccessResult();
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @ApiOperation(value="删除资源",response = RestResponse.class)
    @ApiImplicitParam(name = "id", value = "资源id", required = true, dataType = "String",paramType = "path")
    @DeleteMapping("/{id}")
    public  RestResponse<?> delOne(@PathVariable String id) throws Exception{
        resourceRepository.delete(id);
        return ResponseFactory.createSuccessResult();
    }
}
